import random


def AddDiary (DD,PD,text,cry,emotion,weather,style,OM,UD): #新建日记
    r = DD.AddDiary (PD.u_identity,text,cry,emotion,weather,style)

    if r == 1:
        OM.SaveObj(DD,'./data/DiaryData.pkl')
        UD.LevelUp (4,PD.u_identity)
        OM.SaveObj(UD,'./data/UserData.pkl')

    return r


def OwnerJudge (encry,owner,u_identity,UD): 
#根据读者是否为作者更改界面状态,返回[state,encry_text,decry_disabled,unlock]
    if encry == 1 and owner == u_identity:
        return ['down','加密',True,0,1]
    elif encry == 0 and owner == u_identity:
        return ['normal','不加密',True,0,0]
    if encry == 1 and owner != u_identity:
        unlock = random.randint (0,UD.lock)

        if unlock == 1:
            return ['','',False,1,0]
        else:
            if UD.items[u_identity]['Key'] > 0:
                return ['','',False,0,0]
            else:
                return ['','',True,0,0]
    elif encry == 0 and owner != u_identity:
        return ['','',True,0,0]


def ShowDetails (emotion,weather,style): #控制日记图案显示[emotion,weather,style]
    path = ['','','']
    if emotion == 0:
        path[0] = './UI/detail/positive.png'
    elif emotion == 1:
        path[0] = './UI/detail/neutral.png'
    else:
        path[0] = './UI/detail/negative.png'
                
    if weather == 0:
        path[1] = './UI/detail/sunny.png'
    elif weather == 1:
        path[1] = './UI/detail/cloudy.png'
    elif weather == 2:
        path[1] = './UI/detail/overcast.png'
    elif weather == 3:
        path[1] = './UI/detail/rainy.png'
    else:
        path[1] = './UI/detail/snowy.png'

    path[2] = './UI/style/S' + str(style) + '.png'

    return path


def Encry (encry,DD,PD,page,OM): #为日记加密[encry,encry_text]
    encry_list = []

    if encry == 1:
        DD.DiaryList[PD.u_identity][page]['Encryption'] = 0
        encry_list = [0,'不加密']
    else:
        DD.DiaryList[PD.u_identity][page]['Encryption'] = 1
        encry_list = [1,'加密']
    
    OM.SaveObj(DD,'./data/DiaryData.pkl')

    return encry_list


def Decry (unlock,UD,PD,DD,page,OM): #解锁日记
    if unlock != 1: #非随机数解锁
        UD.items[PD.u_identity]['Key'] -= 1

    OM.SaveObj(DD,'./data/DiaryData.pkl')
    return DD.DiaryList[1-PD.u_identity][page]['Text']
